package collector

import (
	"fmt"
	"github.com/prometheus/client_golang/prometheus"
	"hello/module"
)

type Collector struct {
	value       *prometheus.Desc
	flightinfo  *prometheus.Desc
	flightchage *prometheus.Desc
	srvtask     *prometheus.Desc
	flight      *prometheus.Desc
	taskcache   *prometheus.Desc
	connect     *prometheus.Desc
}

func (d *Collector) Describe(ch chan<- *prometheus.Desc) {
	ch <- d.value
	ch <- d.flightinfo
	ch <- d.flightchage
	ch <- d.srvtask
	ch <- d.flight
	ch <- d.taskcache
	ch <- d.connect
}

func (d *Collector) Collect(ch chan<- prometheus.Metric) {
	c := module.GetCnafMyCat()
	a := module.GetCnafAmq()
	teants := []string{"db_oil_pvg", "db_oil_hza", "db_oil_doy"}
	flightkeys := []string{"OIL_PVG_DIS_FLIGHT", "OIL_HZA_DIS_FLIGHT"}
	taskkeys := []string{"OIL_PVG_DIS_FLIGHT", "OIL_HZA_DIS_FLIGHT"}

	ch <- prometheus.MustNewConstMetric(d.value, prometheus.GaugeValue, c.Value, c.Name)
	ch <- prometheus.MustNewConstMetric(d.connect, prometheus.GaugeValue, a.Connect)
	for _, teant := range teants {
		m := module.GetCnafMySql(teant)
		ch <- prometheus.MustNewConstMetric(d.flightinfo, prometheus.GaugeValue, m.FlightInfo, teant)
		ch <- prometheus.MustNewConstMetric(d.flightchage, prometheus.GaugeValue, m.FlightChage, teant)
		ch <- prometheus.MustNewConstMetric(d.srvtask, prometheus.GaugeValue, m.SrvTask, teant)
	}

	for _, key := range flightkeys {
		fmt.Println(key)
	}
	for _, key := range taskkeys {
		fmt.Println(key)
	}

	//r := module.GetCnafRedis(teant)
	//ch <- prometheus.MustNewConstMetric(d.flight, prometheus.GaugeValue, r.Flight, teant)
	//ch <- prometheus.MustNewConstMetric(d.taskcache, prometheus.GaugeValue, r.Taskcache, teant)
}

func NewMycatCollector() *Collector {
	return &Collector{
		value: prometheus.NewDesc(
			"SrvProperty",
			"检查service库下表srv_property",
			// 动态标签的key列表
			[]string{"name"},
			// 静态标签
			prometheus.Labels{"module": "mycat"},
		),
		flightinfo: prometheus.NewDesc(
			"flightinfo",
			"flightinfo",
			// 动态标签的key列表
			[]string{"teant"},
			// 静态标签
			prometheus.Labels{"module": "mysql"},
		),
		flightchage: prometheus.NewDesc(
			"flightchage",
			"flightchage",
			// 动态标签的key列表
			[]string{"teant"},
			// 静态标签
			prometheus.Labels{"module": "mysql"},
		),
		srvtask: prometheus.NewDesc(
			"srvtask",
			"srvtask",
			// 动态标签的key列表
			[]string{"teant"},
			// 静态标签
			prometheus.Labels{"module": "mysql"},
		),
		flight: prometheus.NewDesc(
			"flight",
			"flight",
			// 动态标签的key列表
			[]string{"teant"},
			// 静态标签
			prometheus.Labels{"module": "redis"},
		),
		taskcache: prometheus.NewDesc(
			"taskcache",
			"taskcache",
			// 动态标签的key列表
			[]string{"teant"},
			// 静态标签
			prometheus.Labels{"module": "redis"},
		),
		connect: prometheus.NewDesc(
			"connect",
			"connect",
			// 动态标签的key列表
			[]string{},
			// 静态标签
			prometheus.Labels{"module": "amq"},
		),
	}
}
